﻿using System;
using System.Windows;
using System.Windows.Input;

namespace Bildschirmpause {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
        Style BigSmile;
        Style Scorn;
        Style Unhappy;
        Style Victory;
        Style What;
        Style AoT;
        Style AoTOff;

        public MainWindow() {
            InitializeComponent();
            dispatcherTimer.Tick += dispatcherTimer_Tick;

            BigSmile = (Style)FindResource("BigSmileImageStyle");
            Scorn = (Style)FindResource("ScornImageStyle");
            Unhappy = (Style)FindResource("UnhappyImageStyle");
            Victory = (Style)FindResource("VictoryImageStyle");
            What = (Style)FindResource("WhatImageStyle");

            AoT = (Style)FindResource("AoTImageStyle");
            AoTOff = (Style)FindResource("AoTOffImageStyle");
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
            this.DragMove();
            e.Handled = true;
        }

        private void CloseImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
            dispatcherTimer.Stop();
            Application.Current.Shutdown();
        }

        private void SettingsImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
            var settingsWindow = new SettingsWindow {
                Owner = this,
                currentConfigOptions = { WorkTimeBase = workTimeBase, BreakTimeBase = breakTimeBase }
            };

            if (settingsWindow.ShowDialog() == true) {

                workTimeBase = settingsWindow.DurationSlider.Value;
                breakTimeBase = settingsWindow.BreakSlider.Value;
                if (settingsWindow.ResetTimerCheckBox.IsChecked == true) {
                    if (pauseWindow != null && pauseWindow.IsLoaded) pauseWindow.Close();
                    isBreak = false;
                    currentTimeBase = workTimeBase;
                    TimeArc.StartAngle = 0;
                    TimeArc.EndAngle = 360;
                    timeSpan = new TimeSpan(0, (int)workTimeBase, 0);
                }
            }
        }

        private FixedStepDispatcherTimer dispatcherTimer = new FixedStepDispatcherTimer(second);

        private TimeSpan timeSpan = new TimeSpan(0, 50, 0);
        private static TimeSpan second = new TimeSpan(0, 0, 0, 1);
        private double workTimeBase = 50;
        private double breakTimeBase = 10;
        private double currentTimeBase = 50;

        private bool isBreak;
        private Emoticon emoticon;

        PauseWindow pauseWindow;

        void dispatcherTimer_Tick(object sender, EventArgs e) {
            timeSpan = timeSpan.Subtract(second);

            if (timeSpan.TotalSeconds == 0) {
                if (isBreak) {
                    // end of break
                    TimeArc.StartAngle = 0;
                    TimeArc.EndAngle = 360;
                } else {
                    // end of work
                    TimeArc.StartAngle = 360;
                    TimeArc.EndAngle = 360;
                }

                // toggle break
                isBreak = !isBreak;

                if (isBreak) {
                    timeSpan = new TimeSpan(0, 0, (int)breakTimeBase, 0);
                    currentTimeBase = breakTimeBase;
                    pauseWindow = new PauseWindow();
                    pauseWindow.Show();
                } else {
                    timeSpan = new TimeSpan(0, 0, (int)workTimeBase, 0);
                    currentTimeBase = workTimeBase;
                    if (pauseWindow.IsLoaded) pauseWindow.Close();
                }
            } else {
                var angle = timeSpan.TotalSeconds * 360 / (currentTimeBase * 60);
                if (isBreak) {
                    if (timeSpan.TotalMinutes < currentTimeBase / 5) {
                        if (emoticon != Emoticon.Scorn) {
                            EmoticonImage.Style = Scorn;
                            emoticon = Emoticon.Scorn;
                        }
                    } else if (timeSpan.TotalMinutes < (currentTimeBase / 5) * 2) {
                        if (emoticon != Emoticon.Unhappy) {
                            EmoticonImage.Style = Unhappy;
                            emoticon = Emoticon.Unhappy;
                        }
                    } else if (timeSpan.TotalMinutes < (currentTimeBase / 5) * 3) {
                        if (emoticon != Emoticon.What) {
                            EmoticonImage.Style = What;
                            emoticon = Emoticon.What;
                        }
                    } else if (timeSpan.TotalMinutes < (currentTimeBase / 5) * 4) {
                        if (emoticon != Emoticon.BigSmile) {
                            EmoticonImage.Style = BigSmile;
                            emoticon = Emoticon.BigSmile;
                        }
                    } else {
                        if (emoticon != Emoticon.Victory) {
                            EmoticonImage.Style = Victory;
                            emoticon = Emoticon.Victory;
                        }
                    }
                    TimeArc.EndAngle = 360 - angle;
                } else {
                    TimeArc.StartAngle = 360 - angle;
                    if (timeSpan.TotalMinutes < currentTimeBase / 5) {
                        if (emoticon != Emoticon.Victory) {
                            EmoticonImage.Style = Victory;
                            emoticon = Emoticon.Victory;
                        }
                    } else if (timeSpan.TotalMinutes < (currentTimeBase / 5) * 2) {
                        if (emoticon != Emoticon.BigSmile) {
                            EmoticonImage.Style = BigSmile;
                            emoticon = Emoticon.BigSmile;
                        }
                    } else if (timeSpan.TotalMinutes < (currentTimeBase / 5) * 3) {
                        if (emoticon != Emoticon.What) {
                            EmoticonImage.Style = What;
                            emoticon = Emoticon.What;
                        }
                    } else if (timeSpan.TotalMinutes < (currentTimeBase / 5) * 4) {
                        if (emoticon != Emoticon.Unhappy) {
                            EmoticonImage.Style = Unhappy;
                            emoticon = Emoticon.Unhappy;
                        }
                    } else {
                        if (emoticon != Emoticon.Scorn) {
                            EmoticonImage.Style = Scorn;
                            emoticon = Emoticon.Scorn;
                        }
                    }
                }
            }
            TimeLabel.Content = "- " + timeSpan;
        }

        enum Emoticon {
            BigSmile,
            Scorn,
            Unhappy,
            Victory,
            What
        }
        private void AboutImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
            var aboutWindow = new AboutWindow { Owner = this };
            aboutWindow.ShowDialog();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e) {
            SnapWindowToTopRight();
        }

        private void SnapWindowToTopRight() {
            var desktopWorkingArea = SystemParameters.WorkArea;
            this.Left = desktopWorkingArea.Right - this.Width;
            this.Top = 0;
        }
        private void AoTImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
            if (this.Topmost) {
                AoTImage.Style = AoTOff;
                this.Topmost = false;
            } else {
                AoTImage.Style = AoT;
                this.Topmost = true;
            }
        }

        private void GoImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
            if (!dispatcherTimer.IsRunning) {
                dispatcherTimer.Start();
            } else {
                dispatcherTimer.Stop();
            }
        }

        private void TopRightImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
            SnapWindowToTopRight();
        }
    }
}
